﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using blackhouse.WebNemo.Business;
using blackhouse.WebNemo.Business.Groups;
using blackhouse.WebNemo.Business.Users;
using blackhouse.WebNemo.Business.Translate;
using System.Globalization;

namespace blackhouse.WebNemo.Web.Controls {
    public partial class Record : System.Web.UI.UserControl {

        #region Fields

        private int groupId;
        private int recordId;
        private Factory factory = Factory.Instance();
        private GroupRecord record;
        private ITranslate translates;

        #endregion

        #region Event Handlers

        protected override void OnInit(EventArgs e) {
            base.OnInit(e);

            this.ReadParameters();
        }

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            this.SetInstance();
            this.PopulateControls();
        }

        #endregion

        #region Methods

        private void ReadParameters() {
            string sGroupId = Request.QueryString["groupId"];
            string sRecordId = Request.QueryString["recordId"];
            if (!string.IsNullOrEmpty(sGroupId) && !string.IsNullOrEmpty(sRecordId)) {
                Int32.TryParse(sGroupId, out this.groupId);
                Int32.TryParse(sRecordId, out this.recordId);
            }
        }

        private void SetInstance() {
            if (this.groupId > 0 && this.recordId > 0) {
                this.record = new GroupRecord(this.groupId, this.recordId, this.GetCurrentCultureInfo());
            }
            this.translates = factory.GetTranslates();
        }

        private void PopulateControls() {
            if (this.record.RecordId == this.recordId && this.record.BelongsToGroupId == this.groupId) {
                this.ltlIdx.Text = this.record.RecordId.ToString();
                this.lnkIdentyficator.Text = this.record.Identyficator;
                this.lnkIdentyficator.ToolTip = this.record.Identyficator;
                this.ltlCreated.Text = string.Format("{0} ({1:F})", WebNemoUtils.GetDisplayUser(this.record.CreatedBy), this.record.CreatedTime);
                this.ltlModified.Text = string.Format("{0} ({1:F})", WebNemoUtils.GetDisplayUser(this.record.LastModifiedBy), this.record.LastModifiedTime);
                this.lnkCancel.NavigateUrl = "/GroupListing.aspx?groupId=" + this.groupId;
                this.lnkCancel.Attributes.Add("data-type", "close-popup");
                //this.lnkEdit.NavigateUrl = NemoUrlUtils.GetUrlUtils().GetEditRecord(this.groupId, this.recordId);
                this.rptData.DataSource = this.record.GetGroup().Columns;
                this.rptData.DataBind();

                this.ddlLanguages.DataSource = this.translates.GetAvalibleLanguages().OrderBy(td => td.DisplayName);
                this.ddlLanguages.DataBind();

                this.ddlLanguages.SelectedValue = GetCurrentCultureInfo().Name;
            }
        }

        private CultureInfo GetCurrentCultureInfo() {
            string ecl;
            HttpCookie hc = Request.Cookies["view-culture"];
            if (hc != null) {
                ecl = hc.Value;
            }
            else
                ecl = "pl-PL";

            try {
                return CultureInfo.GetCultureInfo(ecl);
            }
            catch (Exception) {
                return CultureInfo.GetCultureInfo("pl-PL");
            }

        }

        protected void ddlLanguages_SelectedIndexChanged(object sender, EventArgs e) {
            Response.Cookies.Add(new HttpCookie("view-culture", Request[ddlLanguages.UniqueID]));
            Response.Redirect(Request.RawUrl);
        }

        protected void rptData_ItemDataBound(object sender, RepeaterItemEventArgs e) {
            switch (e.Item.ItemType) {
                case ListItemType.AlternatingItem:
                case ListItemType.Item:
                    GroupColumn column = e.Item.DataItem as GroupColumn;

                    if (column != null) {


                        Panel pnlHolder = e.Item.FindControl("pnlHolder") as Panel;
                        Label lblName = e.Item.FindControl("lblName") as Label;
                        Panel pnlDisplayControl = e.Item.FindControl("pnlDisplayControl") as Panel;

                        if (lblName != null) {
                            lblName.Text = column.Name + ":";
                            lblName.CssClass += " record-display-i-n-" + column.ColumnId;
                        }
                        if (pnlDisplayControl != null) {
                            DisplayRecordValue d = new StringDisplayRecordValue();

                            if (column.ColumnType == FieldType.OneReference) {
                                d = new OneDisplayRecordValue();
                            }

                            if (column.ColumnType == FieldType.ManyReference)
                                d = new ManyDisplayRecordValue();

                            if (column.ColumnType == FieldType.FileField)
                                d = new FileDisplayRecordValue();

                            if (column.ColumnType == FieldType.DateField || column.ColumnType == FieldType.DateTimeField)
                                d = new DateDisplayRecordValue();

                            d.SetControl(column, this.record);
                            if (d.IsEmpty())
                                e.Item.Visible = false;
                            else
                                pnlDisplayControl.Controls.Add(d);
                        }



                    }
                    break;
            }
        }

        #endregion

    }
}